//
//  TicketSortCollection.m
//  lv678
//
//  Created by apple on 15/6/29.
//  Copyright (c) 2015年 lampshade. All rights reserved.
//  旅游票分类控制器

#import "TicketSortCollection.h"
#import "GZTicketModel.h"
#import "AFNetworking.h"
#import "GZTicketSortCell.h"
#import "TicketDetailViewController.h"
#import "GZTicketDeatil.h"
@interface TicketSortCollection(){
    CGFloat viewHeight;
    long cellCount;
}
@end
@implementation TicketSortCollection


- (void)drawRect:(CGRect)rect{
    self.group = [NSMutableArray array];
    //初始化collectionview，并设置collectionview的大小和滚动设置
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];

    self.collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height) collectionViewLayout:flowLayout];
    //设置collectionview的代理
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    
    //设置collectionview的背景色
    [self.collectionView setBackgroundColor:[UIColor whiteColor]];
    [self.collectionView registerClass:[GZTicketSortCell class] forCellWithReuseIdentifier:@"cellIdentity"];
    [self addSubview:self.collectionView];
    [self loadData];
    

}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.group.count;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    GZTicketModel *model          = self.group[indexPath.row];
    static NSString *cellIdentity = @"cellIdentity";
    GZTicketSortCell *cell        = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentity forIndexPath:indexPath];
    cell.userInteractionEnabled   = YES;
    cell.backgroundColor          = [UIColor whiteColor];
    cell.ticket                   = model;
    return  cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat appW = (self.frame.size.width-30)/3;
    return CGSizeMake(appW,30);
}

-(BOOL)collectionView:(UICollectionView *)collectionView shouldSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    return  YES;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 2.0;
}
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    GZTicketModel *model                 = self.group[indexPath.row];
    TicketDetailViewController *detailVc = [[TicketDetailViewController alloc]init];
    detailVc.dataArry                    = model.tripticket;
    [self.delegate pushController:detailVc];
}

- (void)loadData{
    NSString *path        = [[NSBundle mainBundle]pathForResource:@"UrlList.plist" ofType:nil];
    NSDictionary *tempDic = [NSDictionary dictionaryWithContentsOfFile:path];
    _url                  = [tempDic objectForKey:@"travelTicket"];
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    manager.responseSerializer = [AFJSONResponseSerializer serializer];
    manager.requestSerializer  = [AFJSONRequestSerializer serializer];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    NSDictionary *date                                = @{@"serviceName":@"findAllTicket",@"queryParameters":@{}};
    
    dispatch_async(dispatch_get_main_queue(), ^{
        [manager POST:_url parameters:date success:^(AFHTTPRequestOperation *operation, id responseObject) {
            
            NSArray *dateLis     = [responseObject valueForKey:@"dataList"];

            for (NSDictionary *dict in dateLis) {
            GZTicketModel *model = [GZTicketModel ticketWithDtlist:dict];
                [self.group addObject:model];
                cellCount = (self.group.count+3-1)/3;
            }
                [self.collectionView reloadData];
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            //网络提示，后期需改动
            UIAlertView *alert   = [[UIAlertView alloc]initWithTitle:@"错误" message:@"网络连接失败" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [alert show];
        }];
    });
}

@end
